package com.gxust.service.impl;

import com.gxust.dao.OrderDao;
import com.gxust.dto.ProductDto;
import com.gxust.mapper.AddressMapper;
import com.gxust.mapper.CartMapper;
import com.gxust.mapper.OrderMapper;
import com.gxust.mapper.ProductMapper;
import com.gxust.pojo.Cart;
import com.gxust.pojo.Orders;
import com.gxust.pojo.Product;
import com.gxust.service.OrderService;
import com.gxust.vo.OrderSellerVO;
import com.gxust.vo.OrderVO;
import com.gxust.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private ProductMapper productMapper;


    //防止事务中断的数据出错
    //出错就会回滚到执行前
    @Transactional
    @Override
    public void addOrders(OrderDao orderDao) {
        //先插入订单信息
        orderMapper.addOrders(orderDao);
        //这个时候orderDao里的orderId
        Integer orderId = orderDao.getOrderId();
        Integer userId = orderDao.getUserId();
        List<Cart> cartList = orderDao.getProductList();
        for (Cart product : cartList) {
            Integer productId = product.getProductId();
            //插入orderItems表
            orderMapper.addorderitems(orderId,userId,productId);
            //删除购物车数据
            cartMapper.deleteByUserProductId(userId,productId);
        }
    }

    @Transactional
    @Override
    public List<OrderVO> getOrders(Integer userId) {
        ArrayList<OrderVO> orderVOS = new ArrayList<>();
        //先查询Orders
        List<Orders> Orders = orderMapper.getIdByUserId(userId);
        //根据Orders查询商品
        for (Orders order : Orders) {
            OrderVO orderVO = new OrderVO();
            orderVO.setOrder(order);
            //先查productID,和数量
            List<ProductDto> idlist = orderMapper.getProductListByOrderId(order.getOrderId());
            //再查productList
            ArrayList<ProductVO> products = new ArrayList<>();
            for (ProductDto integer : idlist) {
                ProductVO productVO = new ProductVO();
                productVO.setProduct(productMapper.productbyid(integer.getProductId()));
                productVO.setProductNum(integer.getProductNum());
                //单个商品的价格总和
                BigDecimal multiply = productVO.getProduct().getProductPrice().multiply(new BigDecimal(integer.getProductNum()));
                productVO.setProductTotalPrice(multiply);
                products.add(productVO);
            }
            //插入
            orderVO.setProducts(products);
            //查询订单的地址
            orderVO.setAddress(addressMapper.getAddressById(order.getAddressId()));
            orderVOS.add(orderVO);
        }
        return orderVOS;
    }



    /**
     *
     * 商家查询订单
     *
     * @param id
     * @return
     */
    @Override
    public ArrayList<OrderSellerVO> getOrdersBySellerId(Integer id) {
        //最后返回的是一个List<OrderSellerVO>
        ArrayList<OrderSellerVO> orderSellerVOS = new ArrayList<>();
        //要Order,要product要Address,有一个id
        ProductVO productVO = new ProductVO();
        //根据id查询order id groupby orderid
        List<Integer> orderIds = orderMapper.selectOrderIdBySellerId(id);
        Map<Integer,List<ProductVO>>  map = new HashMap<Integer,List<ProductVO>>();
        for (Integer orderId : orderIds) {
            //根据orderID,在orderItems表里查询产品并放到相应的List里
            ArrayList<ProductVO> productVOS = new ArrayList<ProductVO>();
            //得到相应的productId和数量
            List<ProductDto> productListByOrderId = orderMapper.getProductListByOrderId(orderId);
            //根据orderID查询Project
            //创建productList
            for (ProductDto productDto : productListByOrderId) {
                ProductVO productVO1 = new ProductVO();
                productVO1.setProduct(productMapper.productbyid(productDto.getProductId()));
                productVO1.setProductNum(productDto.getProductNum());
                BigDecimal multiply = productVO1.getProduct().getProductPrice().multiply(new BigDecimal(productDto.getProductNum()));
                productVO1.setProductTotalPrice(multiply);
                productVOS.add(productVO1);
            }
            map.put(orderId,productVOS);
            OrderSellerVO orderSellerVO = new OrderSellerVO();
            for (Integer integer : map.keySet()) {
                List<ProductVO> productVOS1 = map.get(integer);
                //根据orderId查询Order数据
                orderSellerVO.setOrder(orderMapper.getOrderByOrderId(integer));
                orderSellerVO.setProducts(productVOS1);
                orderSellerVO.setAddress(addressMapper.getAddressById(orderSellerVO.getOrder().getAddressId()));
                //设置订单总金额
                BigDecimal orderTotalPrice = new BigDecimal(0);
                for (ProductVO vo : productVOS1) {
                    orderTotalPrice = orderTotalPrice.add(vo.getProductTotalPrice());
                }
                orderSellerVO.setOrderTotalPrice(orderTotalPrice);
                //加到要返回的集合里
                orderSellerVOS.add(orderSellerVO);
            }
        }

        return orderSellerVOS;
    }



    public List<OrderSellerVO> getOrdersBySellerId2(Integer id){
        //根据sellerId 查询Orderlist,还要根据时间排序
        //把order和对应的address存进去了
        List<OrderSellerVO> ordersList = orderMapper.selectOrderBySellerId(id);
        for (OrderSellerVO orderSellerVO : ordersList) {
            //查完之后捏,根据orderId和sellerId查产品,和产品数量和产品总价
            List<ProductVO> productVOS = orderMapper.getProductListByOrderIdAndSellerId(orderSellerVO.getOrder().getOrderId(),id);

            //对商家订单的总价进行计算
            BigDecimal totalPrice = new BigDecimal(0);
            for (ProductVO productVO : productVOS) {
                totalPrice=totalPrice.add(productVO.getProductTotalPrice());
            }
            orderSellerVO.setOrderTotalPrice(totalPrice);
            orderSellerVO.setProducts(productVOS);
        }
        return  ordersList;
    }
}
